package cn.com.wind.bdg.collect.collect.service;

import com.alibaba.fastjson.JSON;

import cn.com.wind.bdg.collect.collect.entity.AttachData;
import cn.com.wind.bdg.collect.collect.entity.HBaseModel;
import cn.com.wind.bdg.collect.collect.exception.DataInvalidException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;


@Service
public class UrlContent2HbaseService {
    private static Logger LOGGER = LoggerFactory.getLogger(UrlContent2HbaseService.class);
    @Autowired
    private KafkaService kafkaService;
    @Autowired
    private HBaseService hBaseService;


    public void deal(String content,int i){

//        //kafka相关
//        //1.初始化
//        kafkaService.init();
//        //2.生产消息
//        String key = "wsun.Frank-"+UUID.randomUUID().toString();
//        String msg = "Hello World!";
//        kafkaService.produce(key,msg);
//        //3.消费消息
//        kafkaService.consume();
//        //4.重置偏移量
//        kafkaService.resetOffset();


        //Hbase相关
        String tableNames = "RAW_GAP_21732";
        String rowkey = new String(String.valueOf(i));
        //1.新增记录
        try {
            HBaseModel hBaseModel = new HBaseModel();
            hBaseModel.setTableName(tableNames);
            hBaseModel.setRowkey(rowkey);
            AttachData attachData = new AttachData();
            //设置列族
            attachData.setFamilyName("DATA");
            Map<String,String> dataMap = new HashMap<>();
            //逐个开始添加列
            
//            dataMap.put("UUID", UUID.randomUUID().toString());
            dataMap.put("文本内容", content);
//            dataMap.put("图片名",picName);
//            dataMap.put("图片格式",picContentType);
//            dataMap.put("图片大小",picSize);
            
            
            attachData.setFieldValue(JSON.toJSONString(attachData));
            List<AttachData> list = new ArrayList<>();
            list.add(attachData);
            attachData.setFieldValue(JSON.toJSONString(dataMap));
            hBaseModel.setAttachDataList(list);
            hBaseService.save(hBaseModel);
        } catch (DataInvalidException e) {
            LOGGER.error("消息格式不正確");
        } catch (IOException e) {
            LOGGER.error("其他未知异常。");
        }
        //2.查询记录
//        try {
//            HBaseModel hBaseModel = hBaseService.query(tableNames,rowkey);
//            LOGGER.info("查询到的记录为:"+JSON.toJSONString(hBaseModel));
//        } catch (DataInvalidException e) {
//            LOGGER.error("消息格式不正確");
//        } catch (DataNotFoundException e) {
//            LOGGER.error("未找到rowkey的记录，表名：{} rowke{}",tableNames,rowkey);
//        } catch (IOException e) {
//            LOGGER.error("其他未知异常。");
//        }
//        //3.删除记录
//        try {
//            hBaseService.delete(tableNames,rowkey);
//        } catch (DataInvalidException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            LOGGER.error("其他未知异常。");
//        }
    }
}
